home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / mail / metamail / contrib / ServiceMail / src / pdinq / headers.c next >
Encoding:
C/C++ Source or Header  |  1993-05-09  |  6.6 KB  |  353 lines

  1. /*Copyright (c)  1993 Enterprise Integration Technologies Corporation
  2.  
  3. Permission to use, copy, modify, distribute, and sell this software and
  4. its documentation for any purpose is hereby granted without fee, provided
  5. that (i) the above copyright notices and this permission notice appear in
  6. all copies of the software and related documentation, and (ii) the name of
  7. Enterprise Integration Technologies Corporation may not be used in any
  8. advertising or publicity relating to the software without the specific,
  9. prior written permission of Enterprise Integration Technologies Corporation.
  10.  
  11. THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
  12. EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
  13. WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
  14.  
  15. IN NO EVENT SHALL ENTERPRISE INTEGRATION TECHNOLOGIES CORPORATION  BE
  16. LIABLE FOR ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF
  17. ANY KIND, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  18. PROFITS, WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY
  19. THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  20. PERFORMANCE OF THIS SOFTWARE.
  21. */
  22.  
  23. #include "pdinq.h"
  24.  
  25. extern struct {
  26.   string from;
  27.   time_t date;
  28.   string msgid;
  29. } hdata;
  30.  
  31. extern struct list *firstel;
  32.  
  33. sendmaildata(buf)
  34.   char *buf;
  35.   {
  36.     /*Get the data from the magic tag line that sendmail
  37.       puts on the top of the message*/        
  38.     char *sp;
  39.     struct tm date;
  40.     
  41.     if(strncmp(buf,"From",4))
  42.       goto fail;
  43.   
  44.     if(!(sp=strchr(buf+5,' ')))
  45.       goto fail;
  46.  
  47.     *sp='\0';
  48.     strcpy(hdata.from,buf+5);
  49.     *sp=' ';
  50.  
  51.     strptime(sp+1,"%a %b %d %T %Y",&date);
  52.     hdata.date=timelocal(&date);
  53.     return(1);
  54.  
  55.     fail:
  56.       error(E_SMTAG,buf);
  57.       return(0);
  58.   }
  59.  
  60. line2msgid(buf)
  61.   char *buf;
  62.   {
  63.     strcpy(hdata.msgid,buf+11);
  64.     trimsp(hdata.msgid);
  65.   }
  66.  
  67. line2from(buf)
  68.   char *buf;
  69.   {
  70.     char *begin;
  71.     char *out,*tmp,*place,*end;
  72.     
  73.     if((*buf=='F')||(*buf=='f'))
  74.       begin=buf+4;
  75.     else
  76.       begin=buf+8;
  77.     if((!(tmp=(char *)malloc(strlen(begin))))||(!(out=(char *)malloc(strlen(begin)))))
  78.     {
  79.       error(E_NOMEM,NULL);
  80.       return(0);
  81.     }
  82.     *out=0;    
  83.     while((*begin!='\n')&&(*begin))
  84.     {
  85.       place=tmp;
  86.       end=(gotill(begin,",;\n"));
  87.       while(begin!=end)
  88.         *place++=*begin++;
  89.  
  90.       begin++;
  91.       *place='\0';
  92.       canonicalize(tmp);
  93.       sprintf(out+strlen(out),"%s",tmp);
  94.       if((*begin!='\n')&&(*begin))
  95.         strcat(out,"\t");
  96.     }
  97.     strcpy(hdata.from,out);
  98.   }
  99.  
  100. canonicalize(addr)
  101.   char *addr;
  102.   {
  103.     char *cur,*begin,*tmp;
  104.     int quote,paren,esc,at;
  105.     quote=paren=esc=at=0;
  106.  
  107.     trimsp(addr);
  108.     begin=cur=addr;
  109.     while(*cur)
  110.     {
  111.       if(esc)
  112.       {
  113.         esc=0;
  114.         continue;
  115.       }   
  116.       if(quote)
  117.       {
  118.         if(*cur=='"')
  119.         {
  120.           quote=0;
  121.           if((*++cur)=='@')
  122.           {
  123.             at=1;
  124.             continue;
  125.           }
  126.           else
  127.           {
  128.             SETB(cur);
  129.             continue;
  130.       }
  131.         }
  132.         if(*cur++=='\\')
  133.           esc=1;
  134.         continue;
  135.       }
  136.       switch(*cur)
  137.       {
  138.         case '"':
  139.           quote=1;
  140.           SETB(cur++);
  141.           break;
  142.         case ' ':
  143.         case '\t':
  144.           SETB(++cur);
  145.           break;
  146.         case '(':
  147.           noparen(cur++);
  148.           break;
  149.         case '<':
  150.           if(tmp=gotill(cur,">"))
  151.           {
  152.             cur=tmp;
  153.             cur--;
  154.             begin++;            
  155.             goto doit;
  156.           }
  157.           cur++;
  158.           break;
  159.         case '@':
  160.           at=1;
  161.           cur++;
  162.           break;
  163.         default:
  164.           cur++;
  165.           break;
  166.       }
  167.     }
  168.  
  169.   doit:
  170.       tmp=begin;
  171.       while(tmp!=cur+1)
  172.         *addr++=*tmp++;
  173.       *addr='\0';  
  174.       trimsp(addr);
  175.       return(1);
  176.   }    
  177.  
  178. char *gotill(begin,delim)
  179.   char *begin,*delim;
  180.   {
  181.     char quote,paren,esc;
  182.     quote=paren=esc=0;
  183.  
  184.       while(((!strchr(delim,*begin))||(quote||paren||esc))&&(*begin!='\0'))
  185.       {
  186.         begin++;
  187.         if(esc)
  188.         {
  189.           esc=0;
  190.           continue;
  191.         }
  192.         if(quote)
  193.         {  
  194.           quote^=(*begin=='"');
  195.           continue;
  196.         }
  197.         switch(*begin)
  198.         {
  199.           case '(': paren++;
  200.                     break;
  201.           case ')': paren--;
  202.                     break;
  203.           case '"': quote=1;
  204.                     break;
  205.           case '\\': esc=1;
  206.                     break;
  207.         }                    
  208.       }
  209.       if(*begin=='\0')
  210.         return((char *)NULL);
  211.       return(begin);
  212.   }    
  213.     
  214.  
  215.  
  216. noparen(start)
  217.   char *start;
  218.   {
  219.     int paren,esc;
  220.     char *foo=start;
  221.     paren=esc=0;
  222.     
  223.     while((paren)&&(*foo))
  224.     {
  225.       if(esc)
  226.       {
  227.         esc=0;
  228.         foo++;
  229.         continue;
  230.       }
  231.       switch(*foo++)
  232.       {
  233.         case '(':
  234.           if(!esc)
  235.             paren++;
  236.           break;
  237.         case ')':
  238.           if(!esc)
  239.             paren--;
  240.           break;
  241.         case '\\':
  242.           esc=1;
  243.           break;
  244.       }
  245.     }
  246.     while(*start++=*foo++);
  247.   }
  248.   
  249. line2date(buf)
  250.   char *buf;
  251.   {
  252.     char *dbegin=buf+5; /*Date:....*/
  253.     char *tmp;
  254.     time_t date;
  255.     struct tm time;
  256.     int pos;
  257.     
  258.     /*First crank up getdate() on this....*/
  259.     if((date=getdate(dbegin,(struct timeb*)NULL))>0)
  260.     {
  261.       hdata.date=date;
  262.       return;
  263.     }
  264.  
  265.     /*Then check for the common sendmail style date....*/
  266.     /*Getdate doesn't like a preceding DAY, so lop off everything
  267.     to the first space*/
  268.     tmp=dbegin;
  269.     while(isspace(*++tmp));
  270.     if(!(tmp=strchr(tmp,' ')))
  271.       return;
  272.     tmp++;
  273.     if((date=getdate(tmp,(struct timeb*)NULL))>0)
  274.     {
  275.       hdata.date=date; 
  276.       return;
  277.     }
  278.   }
  279.     
  280. trimsp(buf)
  281.   char *buf;
  282.   {
  283.     char *foo;
  284.     char *bar;
  285.  
  286.     foo=buf-1;
  287.     while(trimmable(*(++foo)));
  288.     bar=buf;
  289.     while(*bar++=*foo++);
  290.     
  291.     bar=strlen(buf)+buf;
  292.     while(trimmable(*(--bar)));
  293.     *(bar+1)='\0';
  294.   }
  295.  
  296. trimmable(a)
  297.   char a;
  298.   {
  299.     if(isspace(a))
  300.       return(1);
  301.     return(0);
  302.   }
  303.  
  304. usefrom(buf)
  305.   char *buf;
  306.   {
  307.     static int replyto=0;
  308.  
  309.     if((!replyto)&&(!strncasecmp(buf,"From",4)))
  310.       return(1);
  311.     if(!strncasecmp(buf,"Reply-to",8))
  312.     {
  313.       replyto=1;
  314.       return(1);
  315.     }
  316.     return(0);
  317.   }
  318.  
  319. stashline(buf)
  320.   char *buf;
  321.   {
  322.     static struct list *curel;
  323.     struct list *new;
  324.     
  325.     new=(struct list *)malloc(sizeof(struct list));
  326.     if(!new)
  327.     {
  328.       error(E_NOMEM,NULL);
  329.       return(0);
  330.     }
  331.     
  332.     if(!firstel)
  333.       curel=firstel=new;
  334.     else
  335.     {
  336.       curel->next=new;
  337.       curel=new;
  338.     }
  339.  
  340.     curel->next=0;
  341.     if(!(curel->line=(char *)malloc(strlen(buf))))
  342.     {
  343.       error(E_NOMEM,NULL);
  344.       return(0);
  345.     }
  346.     strcpy(curel->line,buf);
  347.     return(1);
  348.   }
  349.      
  350.     
  351.       
  352.  
  353.